import { _decorator, Component, director, Node } from 'cc';
const { ccclass, property, executionOrder } = _decorator;

import UI from "../../../Script/Manager/UI";
import S2048_CardFunctions from "./S2048_CardFunctions";
import { S2048_GameData } from "./S2048_GameData";
import S2048_GamePanel from "./S2048_GamePanel";
import { S2048_GameStorage } from "./S2048_GameStorage";
import S2048_Grid from "./S2048_Grid";
import S2048_LosePanel from "./S2048_LosePanel";

// /** GameManager */
// // 慢一点执行
@ccclass('S2048GameManager')
@executionOrder(101)
export default class S2048_GameManager extends Component {
    static Instance: S2048_GameManager;
    @property(Node)
    Root_grid: Node | null = null;
    @property(Node)
    Root_block: Node | null = null;
    onLoad() {
        S2048_GameManager.Instance = this;

        this.Init();
    }
    async Init() {
        S2048_GameStorage.initData();
        S2048_GameData.InitData();

        this.Init_map();

        S2048_GamePanel.Instance.ShowOn();
    }
    async Init_map() {
        await this.Init_grid();
        this.Init_block();
    }
    async Init_grid() {
        for (let x = 0; x < S2048_GameData.col; x++) {
            // S2048_GameData.arrGrids[x] = [];
            for (let y = 0; y < S2048_GameData.row; y++) {
                const id = 0;
                let row = y + 1;
                let col = x + 1;
                const scrGrid: S2048_Grid = await S2048_CardFunctions.CreateGrid(col, row, id);
                // S2048_GameData.arrGrids[x][y] = scrGrid;
                S2048_GameData.arrGrids.push(scrGrid);

            }
        }
    }
    async ReadData() {
        for (let index = 0; index < S2048_GameStorage.data[S2048_GameData.Game_mode].map.length; index++) {
            const item = S2048_GameStorage.data[S2048_GameData.Game_mode].map[index];
            let col = item.col;
            let row = item.row;
            let id = item.id;
            await S2048_CardFunctions.指定地生成一个方块(col, row, id);
        }
    }
    async NewData() {
        let length = 2;
        for (let index = 0; index < length; index++) {
            await S2048_CardFunctions.随机空地生成一个方块(2);
        }
    }
    async Init_block() {
        if (S2048_GameStorage.data[S2048_GameData.Game_mode].map.length > 0) {
            this.ReadData();
        }
        else {
            this.NewData();
        }



        // await S2048_CardFunctions.指定地生成一个方块(1, 1, 2);
        // await S2048_CardFunctions.指定地生成一个方块(1, 2, 2);
        // await S2048_CardFunctions.指定地生成一个方块(1, 3, 2);

        // await S2048_CardFunctions.指定地生成一个方块(2, 1, 2);
        // await S2048_CardFunctions.指定地生成一个方块(3, 1, 2);
        // await S2048_CardFunctions.指定地生成一个方块(4, 1, 2);


        // await S2048_CardFunctions.指定地生成一个方块(1, 4, 4);
        // await S2048_CardFunctions.指定地生成一个方块(2, 4, 2);
        // await S2048_CardFunctions.指定地生成一个方块(3, 4, 2);
        // await S2048_CardFunctions.指定地生成一个方块(4, 4, 2);

        // await S2048_CardFunctions.指定地生成一个方块(4, 1, 4);
        // await S2048_CardFunctions.指定地生成一个方块(4, 2, 2);
        // await S2048_CardFunctions.指定地生成一个方块(4, 3, 2);
        // await S2048_CardFunctions.指定地生成一个方块(4, 4, 2);

        // await S2048_CardFunctions.指定地生成一个方块(1, 1, 4);
        // await S2048_CardFunctions.指定地生成一个方块(1, 2, 2);
        // await S2048_CardFunctions.指定地生成一个方块(1, 3, 2);
        // await S2048_CardFunctions.指定地生成一个方块(1, 4, 2);

        // await S2048_CardFunctions.指定地生成一个方块(1, 1, 2);
        // await S2048_CardFunctions.指定地生成一个方块(1, 2, 2);
        // await S2048_CardFunctions.指定地生成一个方块(1, 3, 2);
        // await S2048_CardFunctions.指定地生成一个方块(1, 4, 4);

        // await S2048_CardFunctions.指定地生成一个方块(1, 1, 2);
        // await S2048_CardFunctions.指定地生成一个方块(1, 2, 2);
        // await S2048_CardFunctions.指定地生成一个方块(1, 3, 2);
        // await S2048_CardFunctions.指定地生成一个方块(1, 4, 2);
    }
    static Main() {
    }
    Lose() {
        UI.Instance.CreatePanel_sub(`S2048_Prefab`, `UI/S2048_LosePanel`, S2048_LosePanel);
    }
    Replay() {
        S2048_GameData.ClearData();

        let currentSceneName = director.getScene().name;
        director.loadScene(currentSceneName);
    }
}

// /** 调试 */
window["S2048_GameManager"] = S2048_GameManager;